import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/equipment_facilities/interlock_list/interlock_details/interlock_details_controller.dart';

/// 联锁清单详情
class InterlockDetailsPage extends StatelessWidget {
  InterlockDetailsPage({super.key});

  final controller = Get.put(InterlockDetailsController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: '联锁清单详情',
            ),
            body: Container(
              alignment: Alignment.topCenter,
              color: const Color(0xffF5F5F6),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    AppGaps.vGap12,
                    Container(
                      height: 2.w,
                      color: const Color(0xFF587DF5),
                    ),
                    Container(
                      alignment: Alignment.centerLeft,
                      color: const Color(0xFFF4F7FF),
                      padding: EdgeInsets.only(left: 40.w, top: 10.w, bottom: 10.w, right: 20.w),
                      child: Text('名称：${controller.instrumentEntity.instrumentLogo}',
                          style: TextStyle(
                            fontSize: 16.sp,
                            color: const Color(0xFF262F38),
                          )),
                    ),
                    Container(
                      height: 1.w,
                      color: const Color(0xFFDEE5FD),
                    ),
                    Container(
                      color: const Color(0xFFffffff),
                      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 14.w),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _getTextView(title: '类型：', value: controller.instrumentEntity.interlockDeviceTypeName!),
                          AppGaps.vGap12,
                          Text(
                            '互锁关系描述:',
                            style: TextStyle(
                              fontSize: 13.sp,
                              height: 1.2,
                              fontWeight: FontWeight.w400,
                              color: const Color(0xffA5A6B5),
                            ),
                          ),
                          AppGaps.vGap8,
                          Container(
                            alignment: Alignment.topLeft,
                            padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.w),
                            decoration: BoxDecoration(
                                color: const Color(0xffF7F7FB),
                                border: Border.all(color: const Color(0xffEBEDF0), width: 1.w),
                                borderRadius: BorderRadius.all(Radius.circular(4.r))),
                            child: Text(
                              controller.instrumentEntity.interlockRelation!,
                              style: TextStyle(
                                fontSize: 14.sp,
                                height: 1.2,
                                fontWeight: FontWeight.w400,
                                color: const Color(0xff495061),
                              ),
                            ),
                          ),
                          AppGaps.vGap12,
                          Text(
                            '互锁逻辑描述:',
                            style: TextStyle(
                              fontSize: 13.sp,
                              height: 1.2,
                              fontWeight: FontWeight.w400,
                              color: const Color(0xffA5A6B5),
                            ),
                          ),
                          AppGaps.vGap8,
                          Container(
                            alignment: Alignment.topLeft,
                            padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.w),
                            decoration: BoxDecoration(
                                color: const Color(0xffF7F7FB),
                                border: Border.all(color: const Color(0xffEBEDF0), width: 1.w),
                                borderRadius: BorderRadius.all(Radius.circular(4.r))),
                            child: Text(
                              controller.instrumentEntity.interlockLogic!,
                              style: TextStyle(
                                fontSize: 14.sp,
                                height: 1.2,
                                fontWeight: FontWeight.w400,
                                color: const Color(0xff495061),
                              ),
                            ),
                          ),
                          AppGaps.vGap12,
                          _getTextView(title: '互锁操作顺序：', value: controller.instrumentEntity.interlockOrder!),
                          AppGaps.vGap12,
                          _getTextView(title: '互锁状态说明：', value: controller.instrumentEntity.interlockStatus!),
                          AppGaps.vGap12,
                          _getTextView(title: '相关故障处理：', value: controller.instrumentEntity.faultHandling!),
                          AppGaps.vGap12,
                          _getTextView(title: '互锁日志：', value: controller.instrumentEntity.interlockLog!),
                          AppGaps.vGap12,
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(
                                width: 110.w,
                                child: Text(
                                  '文件:',
                                  style: TextStyle(
                                    fontSize: 13.sp,
                                    height: 1.2,
                                    fontWeight: FontWeight.w400,
                                    color: const Color(0xffA0A7B5),
                                  ),
                                ),
                              ),
                              Column(
                                children: controller.instrumentEntity.filePathList!.map(_getAttachmentView).toList(),
                              ),
                            ],
                          ),
                          AppGaps.vGap12,
                          _getTextView(title: '是否投用：', value: controller.instrumentEntity.isUsed == '1' ? '是' : '否'),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        });
  }

  /// 获取附件列显示
  Widget _getAttachmentView(String url) {
    //本地文件名
    final String currUrlName = url.toString().substring(25);
    return Container(
      margin: EdgeInsets.only(bottom: 10.w),
      child: Text(
        currUrlName,
        style: TextStyle(
          fontSize: 13.sp,
          color: const Color(0xFF5C6DFF),
        ),
      ).clickOnce(() {
        //打开文件
        FileUtils.openFile(Interface.fileUrl + url);
      }),
    );
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value, Color? valueColor}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 110.w,
          child: Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA0A7B5),
            ),
          ),
        ),
        Expanded(
            child: Text(
          value,
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: valueColor ?? const Color(0xff495061),
          ),
        ))
      ],
    );
  }
}
